
package com.gdpu.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdpu.bean.Cgdingdan;
import com.gdpu.bean.Goods;
import com.gdpu.bean.Stock;
import com.gdpu.bean.Warehouse;
import com.gdpu.common.DataGridView;
import com.gdpu.common.ResultObj;
import com.gdpu.service.CgdingdanService;
import com.gdpu.service.GoodsService;
import com.gdpu.vo.CgdingdanVo;
import com.gdpu.vo.OutdingdanVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.testng.annotations.Test;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/cgdingdan")
public class CgdingdanController {
    @Resource
    private CgdingdanService cgdingdanService;

    //供应商，仓库管理员查询所有角色
    @RequestMapping("loadAllCgdingdan")
    public DataGridView loadAllCgdingdan(CgdingdanVo cgdingdanVo){
        IPage<Cgdingdan> page = new Page<Cgdingdan>(cgdingdanVo.getPage(),cgdingdanVo.getLimit());
        //QueryWrapper可用于查询功能
        QueryWrapper<Cgdingdan> queryWrapper = new QueryWrapper<Cgdingdan>();
        queryWrapper.like(StringUtils.isNotBlank(cgdingdanVo.getGoodsName()),"goods_name",cgdingdanVo.getGoodsName());
        queryWrapper.eq(StringUtils.isNotBlank(cgdingdanVo.getSum()),"sum",cgdingdanVo.getSum());
        queryWrapper.orderByDesc("type");
        cgdingdanService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    //审核员审核采购订单，查询界面
    @RequestMapping("loadAllCgdingdan2")
    public DataGridView loadAllCgdingdan2(CgdingdanVo cgdingdanVo){
        IPage<Cgdingdan> page = new Page<Cgdingdan>(cgdingdanVo.getPage(),cgdingdanVo.getLimit());
        //QueryWrapper可用于查询功能
        QueryWrapper<Cgdingdan> queryWrapper = new QueryWrapper<Cgdingdan>();
        queryWrapper.eq("type","待审核");
        queryWrapper.like(StringUtils.isNotBlank(cgdingdanVo.getGoodsName()),"goods_name",cgdingdanVo.getGoodsName());
        queryWrapper.eq(StringUtils.isNotBlank(cgdingdanVo.getSum()),"sum",cgdingdanVo.getSum());
        cgdingdanService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    //添加
    @RequestMapping("addCgdingdan")
    public ResultObj addCgdingdan(CgdingdanVo cgdingdanVo){
        try {
            cgdingdanService.save(cgdingdanVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }
    //删除
    @RequestMapping("deleteCgdingdan")
    public ResultObj deleteCgdingdan(Integer id){
        try {
            cgdingdanService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
    //供货
    public UpdateWrapper<Cgdingdan> updateWrapper1(CgdingdanVo cgdingdanVo){
        UpdateWrapper<Cgdingdan> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",cgdingdanVo.getId());
        updateWrapper.eq("type","待供应商确认");//type=0：待供应商确认
        updateWrapper.set("type","待审核");//type=1：供应商确认,待审核员审核
        return updateWrapper;
    }

    @RequestMapping("updateCgdingdan")
    public ResultObj updateCgdingdan(CgdingdanVo cgdingdanVo){
        try {
            UpdateWrapper<Cgdingdan> updateWrapper =updateWrapper1(cgdingdanVo);
            cgdingdanService.update(cgdingdanVo,updateWrapper);
            return ResultObj.PROVIDE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.PROVIDE_ERROR;
        }
    }

    //审核员审核采购订单
    public UpdateWrapper<Cgdingdan> updateWrapper2(CgdingdanVo cgdingdanVo){
        UpdateWrapper<Cgdingdan> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",cgdingdanVo.getId());
        updateWrapper.eq("type","待审核");
        updateWrapper.set("type","审核通过");//type=2：审核员确认，待仓库管理员确认
        return updateWrapper;
    }

    @RequestMapping("updateScgdingdan")
    public ResultObj updateScgdingdan(CgdingdanVo cgdingdanVo){
        try {
            UpdateWrapper<Cgdingdan> updateWrapper =updateWrapper2(cgdingdanVo);
            cgdingdanService.update(cgdingdanVo,updateWrapper);
            return ResultObj.Examine_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.Examine_ERROR;
        }
    }

    //仓库管理人员确定最后的采购订单
    public UpdateWrapper<Cgdingdan> updateWrapper3(CgdingdanVo cgdingdanVo){
        UpdateWrapper<Cgdingdan> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",cgdingdanVo.getId());
        updateWrapper.eq("type","审核通过");
        updateWrapper.set("type","订单完成");//type=3：仓库管理员通过采购订单
        return updateWrapper;
    }

    @RequestMapping("updatedingdan")
    public ResultObj updatedingdan(CgdingdanVo cgdingdanVo){
        try {
            UpdateWrapper<Cgdingdan> updateWrapper =updateWrapper3(cgdingdanVo);
            cgdingdanService.update(cgdingdanVo,updateWrapper);
            return ResultObj.Type_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.Type_ERROR;
        }
    }
}



